﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna;
using Microsoft.Xna.Framework;

using FarseerPhysics.Dynamics;

namespace Spacer
{
    public class Planet : Entity
    {
		public Planet() {
            Structures = new List<Item>();
            Inventory = new List<Item>();
		}

        public Planet(SolarSys system, int seed) : base(system)
        {
            Seed = seed;
            Structures = new List<Item>();
            Inventory = new List<Item>();

            Random random = new Random( Seed );
            Radius = (float)random.NextDouble() * 5f + 5f;
            Sprite = Main.Sprites["Planet"+random.Next(1,9)];
            descriptionIndex = random.Next(0, 4);
            Inventory.Add( new Shield(World) );
            Adamantium = random.Next(1000)*(int)Radius;

			if( this.GetType() == typeof(Planet) ) Initialize();
        }

		public override void Initialize() {
			base.Initialize();

			DrawMode = DrawMode.Sprite;
			DrawLayer = Layer.LAYER_18;
            DrawScale = new Vector2((32f / 510f) * Radius, (32f / 510f) * Radius);

            PhysicsInitCircle( Radius, MovementMode.None );
            PhysicsObject.IsSensor = true;

            Invulnerable = true;
		}

		// ----------
		
		public override bool CreateSaveState( EntitySaveState save ) {
			base.CreateSaveState( save );

			save["Seed"] = Seed;
			save["Radius"] = Radius;
			save["Adamantium"] = Adamantium;
			
			List<Guid> inv = new List<Guid>();
			save["InventoryIDs"] = inv;
			foreach(Item i in Inventory) {
				inv.Add( i.ID );
			}
			
			List<Guid> structs = new List<Guid>();
			save["StructureIDs"] = structs;
			foreach(Item s in Structures) {
				structs.Add( s.ID );
			}

			return true;
		}

		public override void PostLoad() {
			List<Guid> inv = (List<Guid>)LoadedSaveState["InventoryIDs"];
			foreach(Guid inv_id in inv) {
				Entity item = null;
				if( (World.AddedEntities.TryGetValue(inv_id,out item) || World.Entities.TryGetValue(inv_id,out item)) && item is Item )
					 Inventory.Add( (Item)item );
			}
			
			List<Guid> structs = (List<Guid>)LoadedSaveState["StructureIDs"];
			foreach(Guid struct_id in structs) {
				Entity structure = null;
				if( World.AddedEntities.TryGetValue(struct_id,out structure) && structure is Item )
					 Structures.Add( (Item)structure );
			}
		}

		// ----------

        public T FindStructureItem<T>() where T : Item { return (T)FindStructureItem(typeof(T)); }
        public Item FindStructureItem(Type type)
        {
            foreach (Item item in Structures)
            {
                if (item.GetType() == type)
                    return item;
            }
            return null;
        }

        public T FindInventoryItem<T>() where T : Item { return (T)FindStructureItem(typeof(T)); }
        public Item FindInventoryItem(Type type)
        {
            foreach (Item item in Inventory)
            {
                if (item.GetType() == type)
                    return item;
            }
            return null;
        }

        public void AddStructure(Item item)
        {
            Structures.Add(item);
        }

        public void RemoveStructure(Item item)
        {
            Structures.Remove(item);
        }

        public override void Update()
        {
            foreach(Extractor item in Structures)
            {
                item.ExtractResourcesFromPlanet(this);
            }
            base.Update();
        }
        //public override void Draw()
        //{
        //    Surface.DrawSubRect(Texture, Position, DrawScale, new Rectangle(0, 0, 1018, 1018), Vector2.Zero, 0f);
        //    base.Draw();
        //}

        public float Radius { get; set; }
        public int Seed { get; set; }
        public int Adamantium { get; set; }
        public List<Item> Structures { get; private set; }
        public List<Item> Inventory { get; private set; }
        public int descriptionIndex;
    }
}
